package com.example.restful.controller;

import com.example.restful.model.Student;
import com.example.restful.model.User;
import com.example.restful.service.StudentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class StudentController {
    @Autowired
    StudentService studentService;
    /**
     * 查询所有用户
     * @return
     */

    @GetMapping("/students")
    @ApiOperation("查询所有用户")
    public List<Student> getAllStudents(){
        return studentService.list();
    }

    /**
     * 根据 id 查询指定用户
     *
     * @param id
     * @return
     */
    @GetMapping("/student/{id}")
    public Student getstudentById(@PathVariable("id") Integer id){
        Student student = studentService.getById(id);
        return student;
    }
    /**
     * 插入用户
     * @param student
     * @return
     */
    @PostMapping("/student")
    public String addStudent(@RequestBody Student student){
        if (studentService.save(student)){
            return "插入用户成功！";
        }
        else return "插入用户失败";
    }
    /**
     * 修改用户信息
     * @param student
     * @return
     */
    @PutMapping("/student")
    public String updateStudent(@RequestBody Student student){
        if (studentService.updateById(student)){
            return "更新用户成功！";
        }
        else return "更新用户失败";
    }
    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/student")
    public String deleteStudentById(Integer id) {
        if (studentService.removeById(id)) {
            return "删除用户成功！";
        } else return "删除用户失败！";
    }
}
